package myLucene;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import matrics.SparseVector;

import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermDocs;
import org.apache.lucene.index.TermFreqVector;
import org.apache.lucene.search.spell.LuceneDictionary;

public class TermFre {

	/**
	 * 
	 * @param iReader
	 * @param term
	 *            x
	 * @return Document frequency of term x
	 * @throws IOException
	 */
	public static int getDF(IndexReader iReader, String term)
			throws IOException {

		return iReader.docFreq(new Term(Props.queryField, term));
	}

	public static double getTF(IndexReader iReader, int docNumber, int start,
			int end, int[] tf,
			LinkedList<Integer>[] dist,String[] intToTerms) throws Exception {
	

		int counterLeft = 1;
		int counterRight = 1;
		int sent = 0;

		// System.out.println("TermFre hit sentence: ");//+ sentWords);
		/**
		 * loop over hit sentence
		 * 
		 */
		// System.out.println(docNumber+" "+Lucene.getDocToString(iReader.document(docNumber)));
		if ((counterLeft <= (Props.windowSize / 2) || counterRight <= Props.windowSize / 2)
				&& (docNumber - sent) > -1
				&& (docNumber + sent) < iReader.maxDoc()) {
			// sentence to the left
			String[] sentWords = iReader.document(docNumber).getValues(
					"content");
			//System.out.println("HIIIIIIIIEEEEEEEr"+start+" "+Lucene.getDocToString(iReader.document(docNumber),"content"));
			//System.out.println("$$$$$$$$$$>>>>>> "+ sentWords[start]);
			String[] sentIndices = iReader.document(docNumber - sent)
					.getValues(Props.queryField);
			/**
			 * * loop handles the left side of the idiom/verbs
			 */
			for (int x = start - 1; x > -1
					&& counterLeft <= (Props.windowSize / 2); x--) {
//				System.out.println("TermFre current word left: " + sentWords[x]
//						+ " " + counterLeft+" index: "+sentIndices[x]);

//					System.out.println(TermFre.class.getName()
//							+ " Hiiiiiiiiiiiiier "
//							+ (tf.get(Integer.parseInt(sentIndices[x])) + 1
//									+ " " + sentWords[x])+" index: "+sentIndices[x]);
				int term =Integer.parseInt(sentIndices[x]);
				tf[term] = tf[term]+1;
				

				// negation because it on the left side of the idiom/verb
				LinkedList<Integer> list = dist[term];
				if(list == null){
					System.out.println("NULLLLLLLLLLLLL "+term + " "+intToTerms[term]);
				}
				list.add(-counterLeft);
				dist[term] = list;
		
				counterLeft++;

			}

			if (counterLeft - 1 > (Props.windowSize / 2)) {
				throw new Exception(
						"Window size exceeded, too far left: class TermFre "
								+ counterLeft);
			}
			/**
			 * handles the right hand side of the loop
			 */

			for (int x = end + 1; x < sentIndices.length
					&& counterRight <= (Props.windowSize / 2); x++) {

				int term = Integer.parseInt(sentIndices[x]);
				
				tf[term] = tf[term]+1;
				LinkedList<Integer> list = dist[term];
				list.add(x-end);
				dist[term] = list;
		
				
				counterRight++;
			}
			if (counterRight - 1 > Props.windowSize / 2) {
				throw new Exception(
						"Window size exceeded, too far right: class TermFre "
								+ counterRight);
			}

		}
		sent++;
//		System.out.println("TermFre docNumber " + docNumber + " " + sent);
		/******************************************************************************************
		 * sentences around the hit sentence
		 ******************************************************************************************/
		while ((counterLeft <= Props.windowSize / 2 && (docNumber - sent) > -1)||
				(counterRight <=Props.windowSize / 2
				&& (docNumber + sent) < iReader.maxDoc()) ){
;
			if(counterLeft <= Props.windowSize / 2 && (docNumber - sent) > -1){
			String[] sentIndices = iReader.document(docNumber - sent)
			.getValues(Props.queryField);
			String[] sentWords = iReader.document(docNumber - sent).getValues(
			"content");


			for (int x = sentIndices.length - 1; x >= 0
					&& counterLeft <= Props.windowSize / 2; x--) {
				int term = Integer.parseInt(sentIndices[x]);
				tf[term]= tf[term]+1; 

				LinkedList<Integer> list = dist[term];
				list.add(-counterLeft);
				dist[term] = list;
		
		

				counterLeft++;

			}
			}
			// Vector<Integer> v= new Vector<Integer>(tf.keySet());
			// Collections.sort(v);
			// for (String s:v){
			// System.out.println("TermFre term freq:"+ s+ tf.get(s));
			// }
			if(counterRight <=Props.windowSize / 2
					&& (docNumber + sent) < iReader.maxDoc()){
			String[] sentIndices = iReader.document(docNumber + sent).getValues(
					Props.queryField);
			String[] sentWords = iReader.document(docNumber +sent).getValues(
			"content");
		

			for (int x = 0; x < sentIndices.length
					&& counterRight <= Props.windowSize / 2; x++) {

				int term = Integer.parseInt(sentIndices[x]);
				tf[term] = tf[term]+1;
				LinkedList<Integer> list = dist[term];
				list.add(counterRight);
				dist[term] = list;
				// System.out.println("TermFre current word right: "+sentLemma[x]);
	
				counterRight++;
			}
			}
	
			// for (String s:v2){
			// System.out.println("TermFre term freq:"+ s+ tf.get(s));
			// }
			sent++;
		}
		if (counterRight - 1 != (Props.windowSize / 2)
				|| counterLeft - 1 != (Props.windowSize / 2)) {
			System.out.println("!!!!!!!!!!!!!!!!!class TermFre: Window to small" + "\n left: "
					+ counterLeft + " right : " + counterRight);
		}
		//TermFre.printTF(tf, intToTerms);
		return -1;

	}



	public static void printTF(int[] tf ,String[] intToTerms) throws Exception{
		for(int x=0;x<tf.length;x++){
			if(tf[x]>0){
				System.out.println(tf[x]+ " "+intToTerms[x]);
			}
			if(tf[x]<0){
				throw new Exception("Negative tf value");
			}
		}
	}
}